package com.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * 功能描述: SHA1加密与map排序
 * @Author: wangzh
 * @Date: 2022/11/3 10:15
 */
public class SHA1Util {
    public static Map<String, String> sortMap(Map<String, String> paramsMap) {
        Map<String, String> treeMap = new TreeMap(new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        treeMap.putAll(paramsMap);
        return treeMap;
    }

    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte[] messageDigest = digest.digest();
            StringBuilder hexString = new StringBuilder();

            for (byte b : messageDigest) {
                String shaHex = Integer.toHexString(b & 255);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }

                hexString.append(shaHex);
            }

            return hexString.toString();
        } catch (NoSuchAlgorithmException var6) {
            var6.printStackTrace();
            return "";
        }
    }
}
